package org.shiro.demo.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.shiro.demo.dao.util.Pagination;
import org.shiro.demo.dao.util.QueryCondition;
import org.shiro.demo.entity.Product;
import org.shiro.demo.entity.ProductManifest;
import org.shiro.demo.entity.ResponseResult;
import org.shiro.demo.service.IBaseService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping(value="/managerProduct")
public class ManageProductController {
	
		@Resource
		private IBaseService baseService;
		
		@RequestMapping("/add")
		@ResponseBody
		private Object addProductCount(@RequestParam(value = "description") String description,
				@RequestParam(value = "price") double price,
				@RequestParam(value = "note") String note,
				@RequestParam(value = "thumbId") String thumbId,
				@RequestParam(value = "imageIds") String imageIds,
				@RequestParam(value = "specification") String specification,
				@RequestParam(value = "amount") int amount,
				@RequestParam(value = "categoryId") long categoryId,
				@RequestParam(value = "id") long id,
				@RequestParam(value="addAmount")long addAmount){
			ResponseResult rr=new ResponseResult();
			
			ProductManifest productManifest=new ProductManifest();
			productManifest.setManifestTime(new Date());
			productManifest.setIn(false);
			productManifest.setAmount(amount);
			
			Product mProduct = baseService.getById(Product.class, id);
			if(mProduct==null){
				rr.setCode(0);
				rr.setInfo("无此产品！");
				return rr;
			}
			
			productManifest.setProduct(mProduct);
			
			try {
				baseService.save(productManifest);
			} catch (Exception e) {
				e.printStackTrace();
				rr.setCode(0);
				rr.setInfo("系统出错");
				return rr;
			}
			
			rr.setCode(1);
			rr.setInfo("success");
			rr.setObject("");
			return rr;
		}
		
		@RequestMapping("/list")
		@ResponseBody
		private Object List(@RequestParam(value = "cpage") Integer currentPage, @RequestParam(value = "pageSize") Integer pageSize,@RequestParam(value="isIn")boolean isIn){
			ResponseResult rr=new ResponseResult();
			
			Pagination<ProductManifest> pagination;
			List<QueryCondition> queryConditions=new ArrayList<QueryCondition>();
			queryConditions.add(new QueryCondition("isIn", QueryCondition.EQ, isIn));
			pagination = baseService.getPagination(ProductManifest.class, queryConditions, "", currentPage, pageSize);
			
			rr.setCode(1);
			rr.setInfo("success");
			rr.setObject(pagination);
			return rr;
		}
		
		@RequestMapping("/active")
		@ResponseBody
		private Object active(@RequestParam(value="id")long id){
			ResponseResult rr=new ResponseResult();
			
			ProductManifest productManifest = baseService.getById(ProductManifest.class, id);
			if(productManifest==null){
				rr.setCode(0);
				rr.setInfo("无此入库订单");
				return rr;
			}
			
			productManifest.setIn(true);
		
			Product product = productManifest.getProduct();
			product.setAmount(product.getAmount()+productManifest.getAmount());
			product.setModifyDate(new Date());
			
			try {
				baseService.update(productManifest);
				baseService.update(product);
			} catch (Exception e) {
				e.printStackTrace();
				rr.setCode(0);
				rr.setInfo("系统出错");
				return rr;
			}
			
			rr.setCode(1);
			rr.setInfo("success");
			rr.setObject("");
			return rr;
		}
}
